package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.XxMemberMapper;
import com.ruoyi.system.domain.XxMember;
import com.ruoyi.system.service.IXxMemberService;

/**
 * 会员Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-08
 */
@Service
public class XxMemberServiceImpl implements IXxMemberService 
{
    @Autowired
    private XxMemberMapper xxMemberMapper;

    /**
     * 查询会员
     * 
     * @param memberId 会员主键
     * @return 会员
     */
    @Override
    public XxMember selectXxMemberByMemberId(Long memberId)
    {
        return xxMemberMapper.selectXxMemberByMemberId(memberId);
    }

    /**
     * 查询会员列表
     * 
     * @param xxMember 会员
     * @return 会员
     */
    @Override
    public List<XxMember> selectXxMemberList(XxMember xxMember)
    {
        return xxMemberMapper.selectXxMemberList(xxMember);
    }

    /**
     * 新增会员
     * 
     * @param xxMember 会员
     * @return 结果
     */
    @Override
    public int insertXxMember(XxMember xxMember)
    {
        xxMember.setCreateTime(DateUtils.getNowDate());
        return xxMemberMapper.insertXxMember(xxMember);
    }

    /**
     * 修改会员
     * 
     * @param xxMember 会员
     * @return 结果
     */
    @Override
    public int updateXxMember(XxMember xxMember)
    {
        xxMember.setUpdateTime(DateUtils.getNowDate());
        return xxMemberMapper.updateXxMember(xxMember);
    }

    /**
     * 批量删除会员
     * 
     * @param memberIds 需要删除的会员主键
     * @return 结果
     */
    @Override
    public int deleteXxMemberByMemberIds(Long[] memberIds)
    {
        return xxMemberMapper.deleteXxMemberByMemberIds(memberIds);
    }

    /**
     * 删除会员信息
     * 
     * @param memberId 会员主键
     * @return 结果
     */
    @Override
    public int deleteXxMemberByMemberId(Long memberId)
    {
        return xxMemberMapper.deleteXxMemberByMemberId(memberId);
    }

    @Override
    public void checkBalance(Long memberId, BigDecimal recordAmount) {
        XxMember xxMember = xxMemberMapper.selectXxMemberByMemberId(memberId);
        if (xxMember == null) {
            throw new ServiceException("会员不存在");
        }
        if (xxMember.getMemberBalanceAmount().compareTo(recordAmount) < 0) {
            throw new ServiceException("余额不足");
        }
    }
}
